#include <iostream>
#include <string>
#include <unistd.h>
#include <mysql/mysql.h>

const std::string host = "127.0.0.1";
const std::string user = "connector";
const std::string passwd = "Caocao123,./";
const std::string db = "conn";
const unsigned int port = 3306;

int main()
{
    // std::cout << "mysql client version:" << mysql_get_client_info() << std::endl;
    MYSQL* my = mysql_init(nullptr);
    if(nullptr == my)
    {
        std::cerr << "init MySQL error" << std::endl;
        return 1;
    }
    
    if(mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        std::cerr << "connect MySQL error" << std::endl;
        return 2;
    }

    mysql_set_character_set(my, "utf8");

    std::string sql = "update user set name='Jimmy' where id=1";
    int n = mysql_query(my, sql.c_str());
    if(n == 0) std::cout << sql << " success" << std::endl;
    else std::cout << sql << " failed" << std::endl;

    MYSQL_RES* res = mysql_store_result(my);
    if(nullptr == res)
    {
        std::cerr << "mysql_store_result error" << std::endl;
        return 4; 
    }
    //sleep(10);
    // std::string sql;
    // while(true)
    // {
    //     std::cout << ">>>";
    //     if(!std::getline(std::cin, sql) || sql == "quit")
    //     {
    //         std::cout << "bye" << std::endl;
    //         break;
    //     }
    //     //if(sql == "quit") break;
    //     int n = mysql_query(my, sql.c_str());
    //     if(n == 0)
    //     {
    //         std::cout << sql << "success:" << n <<std::endl;
    //     } 
    //     else
    //     {
    //         std::cerr << sql << "failed:" << n << std::endl;
    //     }
    // }
    //std::cout << "connect success" << std::endl;
    mysql_close(my);
    return 0;
}